package com.game.raceroom.player.contrl
{
	import flash.utils.getTimer;
	
	import com.util.DEF;
	import com.util.Vector;
	import com.game.raceroom.Room;
	import com.game.raceroom.events.GameEventDispatcher;
	import com.game.raceroom.player.MapInfo;
	import com.game.raceroom.player.Player;
	import com.game.raceroom.player.UserInfo;
	
	import mx.core.UIComponent;
	
	public class ActorContrl extends Player
	{
		public var mapContrl:MapContrl;

		public var currentKey:int = 0;
        public var fix_delay:int=0;
        //public var raceTimer:Label;
        public var keyCode:int;
        public var lastInputTime:Number=0;
		public function ActorContrl(gameCanvas:UIComponent,userInfo:UserInfo,mapInfo:MapInfo,parent:Room)
		{
		    this.isActor=true;
			super(gameCanvas,userInfo,mapInfo,parent);
		}
 
//	    public  function sentPosition():void{
//        	last_keypress_code=keypress_code;		
//        	last_keyrelease_code=keyrelease_code;
//    		this._parent.events.dispatch(GameEventDispatcher.ON_ACTION, this);
//        	this._parent.sortDisplayer();	
//	    }
        var inputTimer:Number=0;
        public var keyInput:Vector=new Vector;
        public function onKeyPress(code:int):void{
      
        	 if(keypress_code==code)return;
        	// if(!validKeyPress(code))return;
			 keyCode= action.getKeyCode(true,false,code);
  	         keypress_code=code;
             keyrelease_code=-1;
             keyInput.put(keyCode);
             //this.keyPress(code);
	         // sentPosition();
			 
        }

        public  function onKeyRelease(code:int):void{
        	 if(keyrelease_code==code)return;
        	// if(!validKeyRelease(code))return;
        	 keyCode= action.getKeyCode(false,true,code);
			 keyrelease_code=code;
			 keypress_code=-1;
			 keyInput.put(keyCode);
			 //this.keyRelease(code);
	       // sentPosition();
	         
        }
//        private function validKeyPress(code:int):Boolean{
//        	if(code<i0)return false;
//	  		 switch (code) {
//	  			 case DEF.DIRECTION_UP:
//	    		    if(_up)return false;
//	    			break;
//	  			 case DEF.DIRECTION_DOWN:
//	    		    if(_down)return false;
//	    			break;
//	    		 case DEF.DIRECTION_LEFT:
//	    		    if(_left)return false;
//	    			break;
//	    		 case DEF.DIRECTION_RIGHT:
//	    		    if(_right)return false;
//	    			break;
//	    	     case DEF.DIRECTION_STOP:
//	    	        if(_brake)return false;
//	    			break;
//	    		 default:
//	    	        return false;
//	         } 
//	         return true; 
//        }
//        private function validKeyRelease(code:int):Boolean{
//        	if(code<i0)return false;
//	  		 switch (code) {
//	  			 case DEF.DIRECTION_UP:
//	    		    if(!_up)return false;
//	    			break;
//	  			 case DEF.DIRECTION_DOWN:
//	    		    if(!_down)return false;
//	    			break;
//	    		 case DEF.DIRECTION_LEFT:
//	    		    if(!_left)return false;
//	    			break;
//	    		 case DEF.DIRECTION_RIGHT:
//	    		    if(!_right)return false;
//	    			break;
//	    	     case DEF.DIRECTION_STOP:
//	    	        if(!_brake)return false;
//	    			break;
//	    		 default:
//	    	        return false;
//	         }  
//	         return true;
//        }
        public function setKeyCode(code:Array){
            for(var i:int=0;i<code.length;i++){
            	this.action.setKeyCode(code[i]);
            }
        }
		public function setUsetInfo(info:UserInfo):void{
			this.userInfo=userInfo;
		}
		public override function refBlood(blood:int):void{
        	if(dead)return;
        	now_blood=blood;
        	if(now_blood<=0){
        		now_blood=0;
        		this.dead=true;
         		this.resetAction();
        		this.deadTime=getTimer();
        		_parent.userStatus.alpha=0.4;
        	}
        	
 
        	trace("blood   "+now_blood+" "+blood);
        	actorDisp.refreshBloodBar(max_blood,now_blood);
        	_parent.userStatus.refreshBloodBar(max_blood,now_blood);
            
        }  
		public function refStr(str:int):void{
        	now_str=str;
        	if(now_str<=0){
        		now_str=0;
        	}
  
        	_parent.userStatus.refreshStrBar(max_str,now_str);
            
        }  
        public  function keyPress(code:int):void{
        	if(code<i0)return;
	  		 switch (code) {
	  			 case DEF.DIRECTION_UP:
	    		    if(_down){
	    		    	_brake_up=true;
	    		    	_brake_down=false;
	    		    }else{
	    		    	_up=true;
	    		    }
	    		    _relaseAccelerator=false;
	    			break;
	  			 case DEF.DIRECTION_DOWN:
	    		    if(_up){
	    		    	_brake_down=true;
	    		    	_brake_up=false;
	    		    }else{
	    		    	_down=true;
	    		    }
	    		    _relaseAccelerator=false;
	    			break;
	    		 case DEF.DIRECTION_LEFT:
	    		    _left=true;
	    		    _right=false;
	    			break;
	    		 case DEF.DIRECTION_RIGHT:
	    		    _left=false;
	    		    _right=true;
	    			break;
	    	     case DEF.DIRECTION_STOP:
	    		    _brake=true;
	    			break;
	    		 default:
	    	        return;
	         }  
	         resetRunParam();
        }

        public  function keyRelease(code:int):void{
        	if(code<i0)return;
	        switch (code) {
	  			 case DEF.DIRECTION_UP:
	    		    _relaseAccelerator=true;
	    		    _brake_up=false;
	    			break;
	  			 case DEF.DIRECTION_DOWN:
	    		    _relaseAccelerator=true;
	    		    _brake_down=false;
	    			break;
	    		 case DEF.DIRECTION_LEFT:
	    		    _left=false;
	    			break;
	    		 case DEF.DIRECTION_RIGHT:
	    		    _right=false;
	    		    break;
	    		 case DEF.DIRECTION_STOP:
	    		    _brake=false;
	    			break;
	    	     default:
	    	        return;
	         }
	         resetRunParam();
		}
	}
}